package com.neusoft.elm.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.w3c.dom.DOMError;

import com.neusoft.elm.dao.BusinessDao;
import com.neusoft.elm.entity.Business;

/**
 * 使用一个Serlvet类应对一批请求，可以急剧降低Servlet类的个数
 * 由 BusinessController 这一个控制器应对所有与商家操作有个的请求
 * @author admin
 *
 */
/**
 * 
 * @author admin
 *
 */
public class BusinessController extends HttpServlet {
	private static final String ACTION_GET = "get";
	private static final String ACTION_ADD = "add";
	private static final String ACTION_GETALL = "getall";

	// get ，post
	/**
	 * 拦截post请求
	 */
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// 接口归一
		doGet(req, resp);
	}

	/**
	 * 拦截get请求 request： 浏览器发送给服务器是所有数据最终被封装到HttpServletRequest 对象中 response ：
	 * 所有服务器发往浏览器是数据都被封装在HttpServletResponse 对象中
	 * 
	 */
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 解决乱码问题
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		// 处理请求
		// 使用request.action表达请求的目的
		// 识别具体的目的
		// 从请求对象中获得数据的方法 【不区分具体的请求方式】
		// request.getParameter能够获得表单数据。返回值只有一种：Stirng
		// 要求所有的请求必须包含action 属性值
		String action = request.getParameter("action");
		// String businessName = request.getParameter("businessName");
		// 把表单数据封装到javabean中
		Business business = f2o(request);
		// 识别请求的目的，完成具体的操作
		switch (action) {
		case ACTION_GET:
			getById(request, response, business);
			break;
		case ACTION_ADD:
			addNewBusiness(request, response, business);
			break;
		case ACTION_GETALL:
			getAll(request, response, business);
			break;
		default:
			doError(request, response);
		}
	}

	private void getAll(HttpServletRequest request, HttpServletResponse response, Business business) {
		//调用模型类完成具体的业务
		BusinessDao businessDao = new BusinessDao();
//		接收业务类的结果
		List<Business> lbs = businessDao.getAll();
//		调用视图完成数据显示
		
//		生成页面
		try {
			PrintWriter out = response.getWriter();
			String html = " <!DOCTYPE html>" + "<html>" + "<head>" + "		<!-- 为机器提供信息的 -->" + ""
					+ "		<meta charset='utf-8'>" + "		<title>商家列表</title>" + "	</head>" + "	<body>"
					+ "		 <table border='1' cellspacing='0' cellpadding='0'>"
					+ "		 	<tr><th>商家名称</th><th>商家地址</th><th>商家描述</th></tr>";
			for (int index = 0; index < lbs.size(); index++) {
				Business business2 = lbs.get(index);
				String btr = "		 	<tr><td>" + business2.getBusinessName() + "</td>"
						+ "<td> " +business2.getBusinessAddress() + "</td>" +
						 "<td>" + business2.getBusinessExplain()
						+ "</td></tr>  ";
				html += btr;
			}
			html += "</table>" + "	</body>" + "" + "</html>  ";
			out.write(html);
			out.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 具体的业务
	 * 
	 * @param request
	 * @param response
	 * @param business
	 */
	private void addNewBusiness(HttpServletRequest request, HttpServletResponse response, Business business) {
		System.out.println(business);
		BusinessDao businessDao = new BusinessDao();
		int i = businessDao.postBusiness(business);

		try {
			PrintWriter out = response.getWriter();
			out.write(business.toString());
			out.write(" <h2> " + i + " </h2> ");
			out.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	/**
	 * 提取Form表单数据，封装到模式类中
	 * 
	 * @param request
	 * @return
	 */
	private Business f2o(HttpServletRequest request) {
		// 判空 ， 尽可能的规避异常
		if (request == null) {
			return null;
		}
		// 准备结果对象
		Business business = new Business();
		String string = request.getParameter("businessId");
		if (string == null || string.trim().length() == 0) {
			// 应该有严格是数据格式判断
		} else {
			business.setBusinessId(Integer.parseInt(string));
		}
		string = request.getParameter("businessName");
		business.setBusinessName(string);

		string = request.getParameter("businessAddress");
		business.setBusinessAddress(string);

		string = request.getParameter("businessExplain");
		business.setBusinessExplain(string);

		string = request.getParameter("orderTypeId");
		if (string == null || string.trim().length() == 0) {
			// 应该有严格是数据格式判断
		} else {
			business.setOrderTypeId(Integer.parseInt(string));
		}

		string = request.getParameter("starPrice");
		if (string == null || string.trim().length() == 0) {
			// 应该有严格是数据格式判断
		} else {
			business.setStarPrice(Float.parseFloat(string));
		}

		string = request.getParameter("deliveryPrice");
		if (string == null || string.trim().length() == 0) {
			// 应该有严格是数据格式判断
		} else {
			business.setDeliveryPrice(Float.parseFloat(string));
		}
		string = request.getParameter("remarks");
		business.setRemarks(string);

		return business;
	}

	private void getById(HttpServletRequest request, HttpServletResponse response, Business business) {
		System.out.println(business);
		try {
			PrintWriter out = response.getWriter();
			out.write(business.toString());
			out.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void doError(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub

	}

}